
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>ES6 (ES2015) · React 基础学习资料</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="小翼">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="02.html" />
    
    
    <link rel="prev" href="00.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    React 手稿
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter01/00.html">
            
                <a href="../chapter01/00.html">
            
                    
                    基础知识
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../chapter01/01.html">
            
                <a href="../chapter01/01.html">
            
                    
                    初识React
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../chapter01/02.html">
            
                <a href="../chapter01/02.html">
            
                    
                    React 特性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../chapter01/03.html">
            
                <a href="../chapter01/03.html">
            
                    
                    React Initial Project
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../chapter01/04.html">
            
                <a href="../chapter01/04.html">
            
                    
                    Component和Dom
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../chapter01/05.html">
            
                <a href="../chapter01/05.html">
            
                    
                    生态组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter02/00.html">
            
                <a href="../chapter02/00.html">
            
                    
                    组件生命周期
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter02/01.html">
            
                <a href="../chapter02/01.html">
            
                    
                    装载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter02/02.html">
            
                <a href="../chapter02/02.html">
            
                    
                    更新
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter02/03.html">
            
                <a href="../chapter02/03.html">
            
                    
                    卸载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter02/04.html">
            
                <a href="../chapter02/04.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter02/05.html">
            
                <a href="../chapter02/05.html">
            
                    
                    React.Component
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter03/00.html">
            
                <a href="../chapter03/00.html">
            
                    
                    state
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter03/01.html">
            
                <a href="../chapter03/01.html">
            
                    
                    Component state
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter03/02.html">
            
                <a href="../chapter03/02.html">
            
                    
                    React-Redux
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter03/03.html">
            
                <a href="../chapter03/03.html">
            
                    
                    React-Router
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter03/04.html">
            
                <a href="../chapter03/04.html">
            
                    
                    redux-saga
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter04/00.html">
            
                <a href="../chapter04/00.html">
            
                    
                    组件其他方法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter04/01.html">
            
                <a href="../chapter04/01.html">
            
                    
                    其他API
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter04/02.html">
            
                <a href="../chapter04/02.html">
            
                    
                    类属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter04/03.html">
            
                <a href="../chapter04/03.html">
            
                    
                    实例属性
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter05/00.html">
            
                <a href="../chapter05/00.html">
            
                    
                    React 高级用法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter05/01.html">
            
                <a href="../chapter05/01.html">
            
                    
                    keys
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter05/02.html">
            
                <a href="../chapter05/02.html">
            
                    
                    事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter05/03.html">
            
                <a href="../chapter05/03.html">
            
                    
                    Refs
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../chapter05/04.html">
            
                <a href="../chapter05/04.html">
            
                    
                    Fragment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../chapter05/05.html">
            
                <a href="../chapter05/05.html">
            
                    
                    Context
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../chapter05/06.html">
            
                <a href="../chapter05/06.html">
            
                    
                    高阶组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../chapter05/07.html">
            
                <a href="../chapter05/07.html">
            
                    
                    类型查检
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../chapter05/08.html">
            
                <a href="../chapter05/08.html">
            
                    
                    代码分隔
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter06/00.html">
            
                <a href="../chapter06/00.html">
            
                    
                    React 实例
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter06/01.html">
            
                <a href="../chapter06/01.html">
            
                    
                    List
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter06/02.html">
            
                <a href="../chapter06/02.html">
            
                    
                    Form
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter06/03.html">
            
                <a href="../chapter06/03.html">
            
                    
                    Form高级
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter06/04.html">
            
                <a href="../chapter06/04.html">
            
                    
                    Tooltip
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter07/00.html">
            
                <a href="../chapter07/00.html">
            
                    
                    React项目打包部署
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter07/01.html">
            
                <a href="../chapter07/01.html">
            
                    
                    与服务端交互
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter07/02.html">
            
                <a href="../chapter07/02.html">
            
                    
                    打包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter07/03.html">
            
                <a href="../chapter07/03.html">
            
                    
                    部署
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="00.html">
            
                <a href="00.html">
            
                    
                    浅谈ES
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.9.1" data-path="01.html">
            
                <a href="01.html">
            
                    
                    ES6 (ES2015)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="02.html">
            
                <a href="02.html">
            
                    
                    ES7 (ES2016)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="03.html">
            
                <a href="03.html">
            
                    
                    ES8 (ES2017)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="04.html">
            
                <a href="04.html">
            
                    
                    ES9 (ES2018)
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter09/00.html">
            
                <a href="../chapter09/00.html">
            
                    
                    React Hooks
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter09/01.html">
            
                <a href="../chapter09/01.html">
            
                    
                    State Hooks useState
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter09/02.html">
            
                <a href="../chapter09/02.html">
            
                    
                    State Hooks useReducer
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >ES6 (ES2015)</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="es6">ES6</h1>
<p>  &#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x4ECB;&#x7ECD;&#x4E00;&#x4E9B;&#x5E38;&#x7528;&#x7684;ES6&#x7684;&#x5185;&#x5BB9;&#x3002;&#x4E5F;&#x662F;&#x6211;&#x4EEC;&#x65E5;&#x5E38;&#x5DE5;&#x4F5C;&#x4E2D;&#x4F7F;&#x7528;&#x6700;&#x591A;&#x7684;&#x3002;</p>
<h2 id="&#x53D8;&#x91CF;&#x5B9A;&#x4E49;">&#x53D8;&#x91CF;&#x5B9A;&#x4E49;</h2>
<p>  &#x62D2;&#x7EDD;&#x4F7F;&#x7528; <code>var</code> &#x6765;&#x5B9A;&#x4E49;&#x3002;&#x91C7;&#x7528; <code>let</code> &#x548C; <code>const</code> &#x3002;</p>
<p>  &#x56E0;&#x4E3A;&#x4E0D;&#x540C;&#x4E8E; <code>var</code> &#x7684;&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;<code>const</code> &#x548C; <code>let</code> &#x90FD;&#x662F;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;&#x3002;</p>
<p>  &#x4E0D;&#x518D;&#x4F7F;&#x7528; <code>var</code>&#xFF0C; &#x4F1A;&#x7ED9;&#x6211;&#x4EEC;&#x7684;&#x4EE3;&#x7801;&#x5E26;&#x6765;&#x5F88;&#x591A;&#x597D;&#x5904;&#xFF0C;&#x6BD4;&#x5982;&#x4EE5;&#x524D;&#x5E38;&#x89C1;&#x7684;&#x53D8;&#x91CF;&#x63D0;&#x5347;&#x95EE;&#x9898;&#x4E5F;&#x5C31;&#x4E0D;&#x5B58;&#x5728;&#x4E86;&#x3002;</p>
<p>  &#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js">  <span class="hljs-keyword">let</span> count = <span class="hljs-number">1</span>;

  count += <span class="hljs-number">1</span>;
</code></pre>
<p>  &#x8FD9;&#x91CC;&#x5217;&#x4E3E;&#x4E00;&#x4E2A;&#x5E38;&#x5728;&#x9762;&#x8BD5;&#x4E2D;&#x51FA;&#x73B0;&#x7684;&#x9898;&#x76EE;&#xFF1A;</p>
<pre><code class="lang-js">  <span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
    setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{<span class="hljs-built_in">console</span>.log(i)}, <span class="hljs-number">0</span>);
  }
</code></pre>
<p>  &#x8BF7;&#x95EE;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x8F93;&#x51FA;&#x4EC0;&#x4E48;&#xFF1F;</p>
<p>  &#x6267;&#x884C;&#x5B8C;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#xFF0C;&#x53D1;&#x73B0;&#x8F93;&#x51FA; 10 &#x4E2A; <code>10</code></p>
<p>  &#x5982;&#x4F55;&#x4FEE;&#x6539;&#x624D;&#x80FD;&#x8BA9;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#x8F93;&#x51FA; 0 ~ 9?</p>
<p>  &#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x5C06; <code>var</code> &#x4FEE;&#x6539;&#x4E3A; <code>let</code> &#x5C31;&#x8981;&#x5427;&#x5B9E;&#x73B0;&#x8F93;&#x51FA; 0 ~ 9 &#x4E86;&#x3002;</p>
<blockquote>
<p>&#x4F7F;&#x7528;&#x539F;&#x5219;&#xFF1A;&#x5B9A;&#x4E49;&#x4E0D;&#x53EF;&#x53D8;&#x7684;&#x53D8;&#x91CF;&#xFF0C;&#x4F7F;&#x7528;  <code>const</code>&#xFF0C;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x4F1A;&#x88AB;&#x4FEE;&#x6539;&#x7684;&#x53D8;&#x91CF;&#x4F7F;&#x7528; <code>let</code>&#x3002;</p>
</blockquote>
<h2 id="&#x5B57;&#x7B26;&#x4E32;&#x6A21;&#x677F;">&#x5B57;&#x7B26;&#x4E32;&#x6A21;&#x677F;</h2>
<p>  &#x5728;ES6&#x4E2D;&#xFF0C;&#x5B9A;&#x4E49;&#x4F7F;&#x7528;&#x4E00;&#x5BF9; ` &#x7B26;&#x53F7;&#xFF08;&#x82F1;&#x6587;&#x8F93;&#x5165;&#x6CD5;&#xFF0C;&#x6570;&#x5B57;1&#x5DE6;&#x8FB9;&#x7684;&#x952E;&#xFF09; &#x5305;&#x88F9;&#x8D77;&#x6765;&#x7684;&#x5185;&#x5BB9;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x6A21;&#x677F;&#xFF0C;&#x53EF;&#x4EE5;&#x4F18;&#x96C5;&#x7684;&#x62FC;&#x63A5;&#x4E00;&#x4E2A;&#x5E26;&#x6709;&#x53D8;&#x91CF;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<p>  &#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js">  <span class="hljs-keyword">const</span> user = <span class="hljs-string">&apos;world&apos;</span>;
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`hello <span class="hljs-subst">${user}</span>`</span>);  <span class="hljs-comment">// hello world</span>

  <span class="hljs-comment">// &#x591A;&#x884C;</span>
  <span class="hljs-keyword">const</span> bookname = <span class="hljs-string">&apos;Raect&apos;</span>;
  <span class="hljs-keyword">const</span> content = <span class="hljs-string">`
    Hello <span class="hljs-subst">${user}</span>,
    Welcome to read <span class="hljs-subst">${bookname}</span>. 
  `</span>;
</code></pre>
<h2 id="&#x9ED8;&#x8BA4;&#x53C2;&#x6570;">&#x9ED8;&#x8BA4;&#x53C2;&#x6570;</h2>
<p>  &#x6211;&#x4EEC;&#x5728;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x5F62;&#x53C2;&#x4E2D;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x9ED8;&#x8BA4;&#x503C;&#x3002;</p>
<p>  &#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js">  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHello</span>(<span class="hljs-params">user = &apos;world&apos;</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`hello <span class="hljs-subst">${user}</span>`</span>);
  }

  sayHello() <span class="hljs-comment">// hello world</span>
</code></pre>
<p>  &#x540C;&#x65F6;&#x4E5F;&#x53EF;&#x4EE5;&#x5F15;&#x7533;&#x5230;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x5B57;&#x9762;&#x91CF;&#x53D8;&#x91CF;&#xFF08;&#x89C1;&#x4E0B;&#x6587;&#xFF09;&#x65F6;&#x8BBE;&#x5B9A;&#x4E00;&#x4E2A;&#x9ED8;&#x8BA4;&#x503C;&#x3002;</p>
<p>  &#x793A;&#x4F8B;:</p>
<pre><code class="lang-js">  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sayHello</span> (<span class="hljs-params">props = {}</span>) </span>{
    <span class="hljs-keyword">const</span> {user = <span class="hljs-string">&apos;world&apos;</span>} = props;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`hello <span class="hljs-subst">${user}</span>`</span>);
  }

  sayHello() <span class="hljs-comment">//hello world</span>
</code></pre>
<h2 id="&#x7BAD;&#x5934;&#x51FD;&#x6570;">&#x7BAD;&#x5934;&#x51FD;&#x6570;</h2>
<p>  js&#x51FD;&#x6570;&#x7684;&#x53E6;&#x4E00;&#x79CD;&#x8BED;&#x6CD5;&#x3002;&#x4E0D;&#x9700;&#x8981;&#x518D;&#x7528; <code>function</code> &#x5173;&#x952E;&#x5B57;&#x6765;&#x58F0;&#x660E;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x7701;&#x7565; <code>return</code> &#x5173;&#x952E;&#x5B57;&#x3002;</p>
<p>  &#x66F4;&#x91CD;&#x8981;&#x7684;&#x662F;&#xFF0C;&#x53EF;&#x4EE5;&#x7EE7;&#x627F;&#x5F53;&#x524D;&#x4E0A;&#x4E0B;&#x6587;&#x7684; <code>this</code> &#xFF0C;&#x8FD9;&#x4E00;&#x70B9;&#x5728;React&#x4E2D;&#x7ECF;&#x5E38;&#x7528;&#x5230;&#x3002;</p>
<p>  &#x793A;&#x4F8B;&#xFF1A;</p>
<p>  &#x4F7F;&#x7528; <code>function</code> &#x58F0;&#x660E;&#x7684;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-js">  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span> (<span class="hljs-params">a, b</span>) </span>{
    <span class="hljs-keyword">return</span> a + b;
  }
</code></pre>
<p>  &#x7BAD;&#x5934;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-js">  (a, b) =&gt; (a + b)
</code></pre>
<p>  &#x5F53;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;<code>()</code> &#x53EF;&#x4EE5;&#x7701;&#x7565;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js">  [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>].map(m =&gt; (m + <span class="hljs-number">1</span>)) <span class="hljs-comment">// [2, 3, 4]</span>
</code></pre>
<p>  &#x5F53;&#x7BAD;&#x5934;&#x51FD;&#x6570;&#x7684;&#x51FD;&#x6570;&#x4F53;&#x590D;&#x6742;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x9700;&#x8981;&#x4F7F;&#x7528; <code>{}</code> &#x5C06;&#x5176;&#x5305;&#x88F9;&#x8D77;&#x6765;&#xFF0C;&#x5E76;&#x4E14;&#x9700;&#x8981;&#x4F7F;&#x7528; <code>return</code> &#x5173;&#x952E;&#x5B57;&#x3002;</p>
<p>  &#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js">  [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>].map((m, index) =&gt; {
    <span class="hljs-keyword">return</span> m + index;
  });
  <span class="hljs-comment">// [1, 3, 5]</span>
</code></pre>
<h2 id="es6&#x7684;&#x5BF9;&#x8C61;&#x548C;&#x6570;&#x636E;">ES6&#x7684;&#x5BF9;&#x8C61;&#x548C;&#x6570;&#x636E;</h2>
<ul>
<li><p>&#x89E3;&#x6784;&#x8D4B;&#x503C;</p>
<p>  &#x89E3;&#x6784;&#x8D4B;&#x503C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4ECE; <code>Object</code> &#x6216;&#x8005; <code>Array</code> &#x4E2D;&#x76F4;&#x63A5;&#x62FF;&#x5230;&#x6570;&#x636E;&#x4F5C;&#x4E3A;&#x53D8;&#x91CF;&#x3002;</p>
<pre><code class="lang-js">  <span class="hljs-comment">//&#x5BF9;&#x8C61;</span>
  <span class="hljs-keyword">const</span> user = {name: <span class="hljs-string">&apos;react&apos;</span>, language: <span class="hljs-string">&apos;js&apos;</span>};
  <span class="hljs-keyword">const</span> {name, language} = user;
  <span class="hljs-built_in">console</span>.log(name, language); <span class="hljs-comment">// react js</span>

  <span class="hljs-comment">//&#x6570;&#x7EC4;</span>
  <span class="hljs-keyword">const</span> list = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>];
  <span class="hljs-keyword">const</span> [foo, baz] = list;
  <span class="hljs-built_in">console</span>.log(foo, baz); <span class="hljs-comment">// 1 2</span>
</code></pre>
<p>  &#x51FD;&#x6570;&#x53C2;&#x6570;&#x4E5F;&#x53EF;&#x4EE5;&#x89E3;&#x6784;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js">  <span class="hljs-keyword">const</span> user = {name: <span class="hljs-string">&apos;react&apos;</span>, language: <span class="hljs-string">&apos;js&apos;</span>};
  <span class="hljs-keyword">const</span> logUser = ({name, language}) =&gt; {
    <span class="hljs-built_in">console</span>.log(name, language);
  };
  logUser(user);
  <span class="hljs-comment">// react js</span>
</code></pre>
<p>  &#x51FD;&#x6570;&#x53C2;&#x6570;&#x5728;&#x89E3;&#x6784;&#x7684;&#x65F6;&#x5019;&#x8FD8;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x522B;&#x540D;&#xFF0C;&#x8BA9;&#x53C2;&#x6570;&#x66F4;&#x6709;&#x610F;&#x4E49;&#xFF1A;</p>
<pre><code class="lang-js">  <span class="hljs-keyword">const</span> logUser = ({payload: user}) =&gt; {
    <span class="hljs-built_in">console</span>.log(user.name);
  }

  logUser({payload: {name: <span class="hljs-string">&apos;react&apos;</span>}});
  <span class="hljs-comment">// react</span>
</code></pre>
</li>
<li><p>&#x5BF9;&#x8C61;&#x7684;&#x5B57;&#x9762;&#x91CF;&#x5B9A;&#x4E49;</p>
<p>  &#x5BF9;&#x8C61;&#x89E3;&#x6784;&#x7684;&#x53CD;&#x5411;&#x64CD;&#x4F5C;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js">  <span class="hljs-keyword">const</span> name = <span class="hljs-string">&apos;react&apos;</span>, language = <span class="hljs-string">&apos;js&apos;</span>;

  <span class="hljs-keyword">const</span> user = {name, language};
  <span class="hljs-comment">//&#x7B49;&#x540C;&#x4E8E;&#xFF1A;</span>
  <span class="hljs-keyword">const</span> otherUser = {name: name, language: language};
</code></pre>
<p>  &#x53E6;&#x5916;&#x5728;&#x5BF9;&#x8C61;&#x4E2D;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x53EF;&#x4EE5;&#x7701;&#x7565; <code>function</code> &#x5173;&#x952E;&#x5B57;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js">  <span class="hljs-keyword">const</span> user = {
    name: <span class="hljs-string">&apos;react&apos;</span>,
    getDetail() {
      <span class="hljs-comment">//do something</span>
    }
  }
  <span class="hljs-comment">//&#x7B49;&#x540C;&#x4E8E;&#xFF1A;</span>
  <span class="hljs-keyword">const</span> otherUser = {
    name: <span class="hljs-string">&apos;react&apos;</span>,
    getDetail: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
      <span class="hljs-comment">// do something</span>
    }
  }
</code></pre>
</li>
</ul>
<h2 id="&#x5C55;&#x5F00;&#x7B26;-">&#x5C55;&#x5F00;&#x7B26; (<code>...</code>)</h2>
<p>  ES6&#x4E2D;&#x53EA;&#x6709;&#x5BF9;&#x6570;&#x7EC4;&#x5B9E;&#x73B0;&#x4E86;&#x8FD9;&#x4E00;&#x529F;&#x80FD;&#xFF0C;ES9&#x5BF9;&#x5BF9;&#x8C61;&#x7684;&#x5C55;&#x5F00;&#x529F;&#x80FD;&#x4E5F;&#x5B9E;&#x73B0;&#x4E86;&#xFF0C;ES9&#x4E4B;&#x524D;&#x9700;&#x8981;&#x52A0;babel&#x8F6C;&#x5316;&#x3002;</p>
<p>  Spread Operator <code>...</code> &#x83B7;&#x53D6;&#x6570;&#x7EC4;&#x6216;&#x5BF9;&#x8C61;&#x7684;&#x9879;&#x76EE;&#x3002;</p>
<p>  &#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js">  <span class="hljs-comment">// &#x6570;&#x7EC4;&#xFF1A;</span>
  <span class="hljs-keyword">const</span> list1 = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>];
  <span class="hljs-keyword">const</span> list2 = [...list1, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>];
  <span class="hljs-built_in">console</span>.log(list2);
  <span class="hljs-comment">//[1, 2, 3, 4, 5]</span>

  <span class="hljs-comment">//&#x5BF9;&#x8C61;</span>
  <span class="hljs-keyword">const</span> obj1 = {name: <span class="hljs-string">&apos;react&apos;</span>, language: <span class="hljs-string">&apos;js&apos;</span>};
  <span class="hljs-keyword">const</span> obj2 = {user: <span class="hljs-string">&apos;facebook&apos;</span>, name: <span class="hljs-string">&apos;react&apos;</span>, ...obj1};
  <span class="hljs-built_in">console</span>.log(obj2);
  <span class="hljs-comment">//{user: &quot;facebook&quot;, name: &quot;react&quot;, language: &quot;js&quot;}</span>
</code></pre>
<blockquote>
<p>&#x5C55;&#x5F00;&#x7B26;&#x7684;&#x64CD;&#x4F5C;&#x4E2D;&#xFF0C;&#x6267;&#x884C;&#x7684;&#x662F;&#x4ECE;&#x53F3;&#x5411;&#x5DE6;&#x7684;&#x5408;&#x5E76;&#x64CD;&#x4F5C;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x5982;&#x679C;&#x6709;&#x5BF9;&#x8C61;&#x7684; <code>key</code> &#x76F8;&#x540C;&#xFF0C;&#x5219;&#x4F1A;&#x88AB;&#x53F3;&#x8FB9;&#x7684;&#x8986;&#x76D6;&#x3002;</p>
</blockquote>
<p>  &#x8FD9;&#x4E00;&#x529F;&#x80FD;&#x5728;React&#x4E2D;&#x7ECF;&#x5E38;&#x4F7F;&#x7528;&#xFF0C;&#x6BD4;&#x5982;<code>props</code>&#x7684;&#x6269;&#x5C55;&#x3002;</p>
<p>  &#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-jsx">  (props) =&gt; {
    <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">MyComponent</span> {<span class="hljs-attr">...props</span>}/&gt;</span>
  }
</span></code></pre>
<h2 id="promise">Promise</h2>
<p>  Promise&#x662F;&#x5BF9;&#x4E4B;&#x524D;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x51FD;&#x6570;&#x56DE;&#x8C03;&#x5904;&#x7406;&#x5F02;&#x6B65;&#x65B9;&#x6CD5;&#x7684;&#x4E00;&#x79CD;&#x4F18;&#x96C5;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;&#x3002;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js">  fetch(<span class="hljs-string">&apos;/api/users&apos;</span>)
  .then(res =&gt; res.json())
  .then(data =&gt; data)
  .catch(err =&gt; <span class="hljs-built_in">console</span>.error(err));
</code></pre>
<p>  &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;Promise:</p>
<pre><code class="lang-js">  <span class="hljs-keyword">const</span> delay = (timeout) =&gt; {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((resolve, rejected) =&gt; {
      <span class="hljs-keyword">if</span>(timeout &lt; <span class="hljs-number">0</span>) {
        rejected();
      } <span class="hljs-keyword">else</span> {
       setTimeout(resolve(), timeout);
      }
    });
  };

  delay(<span class="hljs-number">1000</span>).then(() =&gt; {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;executed&apos;</span>);
  });
</code></pre>
<h2 id="objectassgin">Object.assgin()</h2>
<p>  <code>Object.assgin()</code> &#x4E5F;&#x662F;&#x6211;&#x4EEC;&#x7F16;&#x7801;&#x4E2D;&#x5E38;&#x7528;&#x7684;&#x3002;&#x591A;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x5408;&#x5E76;&#xFF0C;&#x5BF9;&#x8C61;&#x7684;&#x6DF1;&#x62F7;&#x8D1D;&#x3002;</p>
<p>  &#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-js">  <span class="hljs-keyword">const</span> obj1 = {name: <span class="hljs-string">&apos;react&apos;</span>};
  <span class="hljs-keyword">const</span> obj2 = <span class="hljs-built_in">Object</span>.assgin({}, obj1, {language: <span class="hljs-string">&apos;js&apos;</span>});
</code></pre>
<p>  &#x5F53;&#x7136;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>...</code>&#x5C55;&#x5F00;&#x7B26;&#x6765;&#x4EE3;&#x66FF;.</p>
<pre><code class="lang-js">  <span class="hljs-keyword">const</span> obj1 = {name: <span class="hljs-string">&apos;react&apos;</span>};
  <span class="hljs-keyword">const</span> obj2 = {...obj1, language: <span class="hljs-string">&apos;js&apos;</span>});
</code></pre>
<h2 id="forof-&#x5FAA;&#x73AF;">for...of &#x5FAA;&#x73AF;</h2>
<p>  <code>for...of</code>&#x5728;&#x5FAA;&#x73AF;&#x4E2D;&#x5FAA;&#x73AF;&#x662F;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;, <code>for...of</code> &#x5141;&#x8BB8;&#x4F60;&#x904D;&#x5386; <code>Arrays</code>&#xFF08;&#x6570;&#x7EC4;&#xFF09;, <code>Strings</code>&#xFF08;&#x5B57;&#x7B26;&#x4E32;&#xFF09;, <code>Maps</code>&#xFF08;&#x6620;&#x5C04;&#xFF09;, <code>Sets</code>&#xFF08;&#x96C6;&#x5408;&#xFF09;&#x7B49;&#x53EF;&#x8FED;&#x4EE3;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x7B49;&#x3002;</p>
<p>  &#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js">  <span class="hljs-keyword">const</span> list = [<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>];
  <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> item <span class="hljs-keyword">of</span> list) {
    <span class="hljs-keyword">if</span>(item % <span class="hljs-number">2</span> === <span class="hljs-number">0</span>) {
      <span class="hljs-keyword">break</span>;
    }
    <span class="hljs-built_in">console</span>.log(item);
  }
  <span class="hljs-comment">//1</span>
  <span class="hljs-comment">//3</span>
</code></pre>
<h2 id="class">class</h2>
<p>  ES6&#x63D0;&#x51FA;&#x7C7B;&#x7684;&#x6982;&#x5FF5;&#xFF0C;&#x4F5C;&#x4E3A;&#x5BF9;&#x8C61;&#x7684;&#x6A21;&#x677F;&#x3002;&#x901A;&#x8FC7; <code>class</code> &#x5173;&#x952E;&#x5B57;&#xFF0C;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x7C7B;&#x3002;</p>
<p>  &#x57FA;&#x672C;&#x4E0A;&#xFF0C;ES6 &#x7684; <code>class</code> &#x53EF;&#x4EE5;&#x770B;&#x4F5C;&#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x8BED;&#x6CD5;&#x7CD6;&#xFF0C;&#x5B83;&#x7684;&#x7EDD;&#x5927;&#x90E8;&#x5206;&#x529F;&#x80FD;&#xFF0C;ES5 &#x90FD;&#x53EF;&#x4EE5;&#x505A;&#x5230;&#xFF0C;&#x65B0;&#x7684;<code>class</code>&#x5199;&#x6CD5;&#x53EA;&#x662F;&#x8BA9;&#x5BF9;&#x8C61;&#x539F;&#x578B;&#x7684;&#x5199;&#x6CD5;&#x66F4;&#x52A0;&#x6E05;&#x6670;&#x3001;&#x66F4;&#x50CF;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x7F16;&#x7A0B;&#x7684;&#x8BED;&#x6CD5;&#x800C;&#x5DF2;&#x3002;</p>
<p>  &#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-js">  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
    name: <span class="hljs-string">&apos;&apos;</span>,
    age: <span class="hljs-string">&apos;&apos;</span>,
    <span class="hljs-keyword">constructor</span>() {
    }
  }
</code></pre>
<p>  &#x7C7B;&#x8FD8;&#x53EF;&#x4EE5;&#x7EE7;&#x627F;&#xFF0C;&#x901A;&#x8FC7; <code>extends</code> &#x5173;&#x952E;&#x5B57;&#x3002;</p>
<pre><code class="lang-js">  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Person</span> </span>{

  }
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4E3A;&#x6211;&#x4EEC;&#x5F00;&#x53D1;&#x4E2D;&#x5E38;&#x89C1;&#x7684;ES6&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x8FD9;&#x4E9B;&#x5185;&#x5BB9;&#x5728;React&#x7684;&#x5F00;&#x53D1;&#x4E2D;&#x4F1A;&#x7ECF;&#x5E38;&#x7528;&#x5230;&#x3002;&#x540C;&#x65F6;&#x4E5F;&#x5EFA;&#x8BAE;&#x6211;&#x4EEC;&#x5C3D;&#x91CF;&#x4F7F;&#x7528;ES6&#x8BED;&#x6CD5;&#x6765;&#x7F16;&#x5199;JS&#x4EE3;&#x7801;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="00.html" class="navigation navigation-prev " aria-label="Previous page: 浅谈ES">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="02.html" class="navigation navigation-next " aria-label="Next page: ES7 (ES2016)">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"ES6 (ES2015)","level":"1.9.1","depth":2,"next":{"title":"ES7 (ES2016)","level":"1.9.2","depth":2,"path":"chapter08/02.md","ref":"./chapter08/02.md","articles":[]},"previous":{"title":"浅谈ES","level":"1.9","depth":1,"path":"chapter08/00.md","ref":"./chapter08/00.md","articles":[{"title":"ES6 (ES2015)","level":"1.9.1","depth":2,"path":"chapter08/01.md","ref":"./chapter08/01.md","articles":[]},{"title":"ES7 (ES2016)","level":"1.9.2","depth":2,"path":"chapter08/02.md","ref":"./chapter08/02.md","articles":[]},{"title":"ES8 (ES2017)","level":"1.9.3","depth":2,"path":"chapter08/03.md","ref":"./chapter08/03.md","articles":[]},{"title":"ES9 (ES2018)","level":"1.9.4","depth":2,"path":"chapter08/04.md","ref":"./chapter08/04.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":[],"root":"./books","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"小翼","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"React 基础学习资料","language":"zh-hans","links":{"gitbook":false,"sharing":{"google":true,"facebook":true,"twitter":true}},"gitbook":"*"},"file":{"path":"chapter08/01.md","mtime":"2020-03-07T07:33:43.872Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-03-09T08:08:41.080Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

